import * as API from '@/api/workmanship'
import { ref, reactive, onBeforeMount, watch } from 'vue'
import dayjs from 'dayjs'

const dicts = {
  left1: {
    dictCode: 'large_screen_left',
    itemName: '一次能源',
  },
  left2: {
    dictCode: 'large_screen_left',
    itemName: '自来水用量及单耗',
  },
  left3: {
    dictCode: 'large_screen_left',
    itemName: 'N2用量及单耗',
  },
  left4: {
    dictCode: 'large_screen_left',
    itemName: '天然气用量及单耗',
  },
  left5: {
    dictCode: 'large_screen_left',
    itemName: '化学品单耗',
  },
  left6: {
    dictCode: 'large_screen_left',
    itemName: '电能单耗及产量',
  },
  middleTop: {
    dictCode: 'large_screen_energy_consumption_proportion_of_each_branch_factory',
    itemName: '各分厂能耗占比',
  },
  middle1: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'ARRAY',
  },
  middle2: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'ARRAY',
  },
  middle3: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'ARRAY',
  },
  middle4: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CF',
  },
  middle5: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CF',
  },
  middle6: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CF',
  },
  middle7: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CELL',
  },
  middle8: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CELL',
  },
  middle9: {
    dictCode: 'da_ping_energy_consumption_of_each_branch_factory',
    itemName: 'CELL',
  },
  right1: {},
  right2: {
    dictCode: 'large_screen_right',
    itemName: '压缩空气',
  },
  right3: {
    dictCode: 'large_screen_right',
    itemName: '纯废水',
  },
  right4: {
    dictCode: 'large_screen_right',
    itemName: '动力',
  },
}

const getQueryParams = (dictKey: any) => {
  return {
    ...dicts[dictKey],
    queryType: 'sevenDay',
    endDay: dayjs().format('YYYY-MM-DD'),
  }
}
const testData = {
  xaxisList: [
    '2024-07-12',
    '2024-07-13',
    '2024-07-14',
    '2024-07-15',
    '2024-07-16',
    '2024-07-17',
    '2024-07-18',
  ],
  yaxisList: [
    {
      name: '电能单耗及产量',
      data: [1, 2, 3, 2, 4, 5, 3],
    },
  ],
}
export default (params: any) => {
  const { energyType, dateEnergy, dateProductionLine, dateClassData, dateDevice, dateUnit, searchParam } = params
  const data = reactive({
    a: testData,
    energy: null,
    productionLine: null,
    device: null,
    unit: null,
    classData: null
  })
  const getParams = (date) => {
    const obj: any = {
      energyType: searchParam.value.energyType,
      departmentClassifyId: searchParam.value.depart,
      productionLineClassifyIds: searchParam.value.unit,
      deviceClassifyIds: searchParam.value.unit,
      unitClassifyIds: searchParam.value.unit,
      dateType: date.value.dateType,
    }
    if (date.value.dateType === 'day') {
      obj.startDate = dayjs(date.value.dateDay).format('YYYY-MM-DD')
      obj.endDate = dayjs(date.value.dateDay).format('YYYY-MM-DD')
    } else {
      obj.startDate = dayjs(date.value.date).format('YYYY-MM')
    }
    return obj
  }
  // const getEnergy = async () => {
  //   const _params = getParams(dateEnergy)
  //   const res = await API.getEnergy(_params)
  //   if (res) {
  //     data.energy = res
  //   }
  // }
  const getProductionLine = async () => {
    const _params = getParams(dateProductionLine)
    const params = {
      startDay: _params.startDate,
      endDay: _params.endDate,
      queryType: _params.dateType,
      dataTypeId: searchParam.value.energyType,
      classifyIdList: searchParam.value.unit,
    }
    const res = await API.getProductionLine(params)
    if (res) {
      data.productionLine = res
    }
  }
  const getDeviceVo = async () => {
    const _params = getParams(dateDevice)
     const params = {
      startDay: _params.startDate,
      endDay: _params.endDate,
      queryType: _params.dateType,
      dataTypeId: searchParam.value.energyType,
      classifyIdList: searchParam.value.unit,
    }
    const res = await API.getDeviceVo(params)
    if (res) {
      data.device = res
    }
  }
  const getClassData = async () => {
    const _params = getParams(dateClassData)
    const params = {
      startDay: _params.startDate,
      endDay: _params.endDate,
      queryType: _params.dateType,
      dataTypeId: searchParam.value.energyType,
      classifyId: searchParam.value.depart,
    }
    const res = await API.getTotalAllLevelsBelowByClassifyId(params)
    if (res) {
      data.classData = res
    }
  }
  // const getUnitVo = async () => {
  //   const _params = getParams(dateUnit)
  //   const res = await API.getUnitVo(_params)
  //   if (res) {
  //     data.unit = res
  //   }
  // }
  const getData = () => {
    // getEnergy()
    if (searchParam.value?.unit?.length) {
      getProductionLine()
    }
    if (searchParam.value?.device) {
      getDeviceVo()
    }
    getClassData()
    // getUnitVo()
  }
  onBeforeMount(() => {
    // getData();
  })
  return {
    data,
    // getEnergy,
    getProductionLine,
    getDeviceVo,
    // getUnitVo,
    getClassData,
    getData,
  }
}
